package com.apes.fn.scm.lossSale;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.framework.query.ApesDao;
import org.springframework.stereotype.Component;

/**
 * @program:
 * @description:
 * @author: lws
 * @create: 2023/2/20 17:56
 **/
@Component
public class LossSaleAdjustDao extends ApesDao {

    public JSONArray lossSaleAdjustFindAll(JSONObject param) {
        StringBuilder sb = new StringBuilder();
        sb.append("SELECT PAGE.*\n" +
                "FROM (SELECT TAB.*, ROWNUM AS NUM\n" +
                "      FROM ( SELECT A.ID ");
        sb.append(querySplicing());
        conditionSplicing(param,sb);
        sb.append("  ORDER BY A.ID DESC) TAB\n" +
               "        WHERE ROWNUM <= :max) PAGE\n" +
               "  WHERE PAGE.NUM >= :min");
        return toJSONArray(find(sb.toString(),param));
    }


    public JSONArray lossSaleAdjustFindAllCount(JSONObject param) {
        StringBuilder sb = new StringBuilder();
        sb.append(" SELECT count(1) count ");
        sb.append(querySplicing());
        conditionSplicing(param,sb);
        return toJSONArray(find(sb.toString(),param));
    }

    private void conditionSplicing(JSONObject param,StringBuilder sql){
        if (param.containsKey("id"))                sql.append(" AND A.ID = :id");
        if (param.containsKey("lossQuotationId"))   sql.append(" AND A.loss_Quotation_Id = :lossQuotationId");
        if (param.containsKey("carLicense"))  sql.append(" AND (select m.car_license from fn_customer_expand m where m.id = a.customer_expand_id) = :carLicense");
        if (param.containsKey("deptId"))           sql.append(" AND (A.DEPT_ID = :deptId or A.GET_CAR_DEPT_ID = :deptId )");
        if (param.containsKey("state"))             sql.append(" AND A.state = :state");
        if (param.containsKey("createDate"))        sql.append(" AND A.create_date >= to_date(:createDateGTE,'yyyy-MM-dd HH24:mi:ss') AND A.create_date <= to_date(:createDateLTE,'yyyy-MM-dd HH24:mi:ss')");
        if (param.containsKey("dateApprove"))       sql.append(" AND A.date_approve >= to_date(:dateApproveGTE,'yyyy-MM-dd HH24:mi:ss') AND A.date_approve <= to_date(:dateApproveLTE,'yyyy-MM-dd HH24:mi:ss')");
    }

    private String querySplicing(){
        return "    FROM LOSS_SALE_ADJUST A\n" +
                "            WHERE A.STATE != 'delete'\n" +
                "              AND ((EXISTS(SELECT 1\n" +
                "                           FROM AUT_POSTS ROLE1,\n" +
                "                                AUT_LOCAL_PERMIT ROLE2,\n" +
                "                                AUT_LOCAL_PERMIT_ITEM ROLE3\n" +
                "                           WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID\n" +
                "                             AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID\n" +
                "                             AND ROLE1.ID = :postId\n" +
                "                             AND ROLE3.TYPE = 'pt_dept'\n" +
                "                             AND ROLE3.PURPOSE = 'pp_search'\n" +
                "                             AND A.DEPT_ID = ROLE3.ORG_ID) OR (SELECT NVL(COUNT(1), 0)\n" +
                "                                                               FROM AUT_POSTS ROLE1,\n" +
                "                                                                    AUT_LOCAL_PERMIT ROLE2,\n" +
                "                                                                    AUT_LOCAL_PERMIT_ITEM ROLE3\n" +
                "                                                               WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID\n" +
                "                                                                 AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID\n" +
                "                                                                 AND ROLE1.ID = :postId\n" +
                "                                                                 AND ROLE3.TYPE = 'pt_dept'\n" +
                "                                                                 AND ROLE3.PURPOSE = 'pp_search') = 0) OR\n" +
                "                   (EXISTS(SELECT 1\n" +
                "                           FROM AUT_POSTS ROLE1,\n" +
                "                                AUT_LOCAL_PERMIT ROLE2,\n" +
                "                                AUT_LOCAL_PERMIT_ITEM ROLE3\n" +
                "                           WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID\n" +
                "                             AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID\n" +
                "                             AND ROLE1.ID = :postId\n" +
                "                             AND ROLE3.TYPE = 'pt_dept'\n" +
                "                             AND ROLE3.PURPOSE = 'pp_search'\n" +
                "                             AND A.GET_CAR_DEPT_ID = ROLE3.ORG_ID) OR (SELECT NVL(COUNT(1), 0)\n" +
                "                                                               FROM AUT_POSTS ROLE1,\n" +
                "                                                                    AUT_LOCAL_PERMIT ROLE2,\n" +
                "                                                                    AUT_LOCAL_PERMIT_ITEM ROLE3\n" +
                "                                                               WHERE ROLE1.LOCAL_PERMIT_ID = ROLE2.ID\n" +
                "                                                                 AND ROLE2.ID = ROLE3.LOCAL_PERMIT_ID\n" +
                "                                                                 AND ROLE1.ID = :postId\n" +
                "                                                                 AND ROLE3.TYPE = 'pt_dept'\n" +
                "                                                                 AND ROLE3.PURPOSE = 'pp_search') = 0))";
    }
}
